﻿/**
   import com.peter.Csv;
   import com.peter.CsvEvent;
   import flash.events.Event;
   var csv:Csv=new Csv();
   csv.loadURL("online3.csv");
   csv.addEventListener(Event.COMPLETE,onLoadCompleteHandler);
   csv.addEventListener(IOErrorEvent.IO_ERROR,onLoadIOError);
   csv.addEventListener(CsvEvent.EOF_ERROR,onReadFileError);
   function onLoadCompleteHandler(event:Event):void {
   var arr:Array=csv.getRandomRecords(10);
   for (var i:uint=0; i<arr.length; i++) {
   trace("q:"+arr[i]["question"]+"__A:"+arr[i]["a"]);//question,a為csv第一行相應列對應記錄字段
   }
   }
   function onLoadIOError(event:IOErrorEvent):void{
   trace('url error');
   }
   function onReadFileError(event:CsvEvent):void{
   trace('read csv file error');
   }
 *******************************************************/
package org.zengrong.cvsfile{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	import flash.errors.EOFError;

	[Event(name="complete",type="flash.events.Event")]
	[Event(name="ioError",type="flash.events.IOErrorEvent")]
	public class CSV extends EventDispatcher {
		private var _records:Array = new Array;
		private var _length:int = 0;
		private const DQUOTES:String = "#100"; //\"
		private const SEMICOLON:String = "#101";
		private var _currentIndex:uint = 0;
		private var _encoding:String = "utf8";

		public function CSV(){
		}

		public function loadURL(url:String):void {
			_records = new Array;
			var loader:URLLoader = new URLLoader;
			var link:URLRequest = new URLRequest(url);
			loader.addEventListener(Event.COMPLETE, onLoadCompleteHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadErrorHandler);
			loader.dataFormat = "binary";
			loader.load(link);
		}

		/**解析csv内容*/
		private function onLoadCompleteHandler(event:Event):void {
			var byteArray:ByteArray = ByteArray(event.target.data);
			var resultStr:String = ""; //文件的内容
			try {
				resultStr = byteArray.readMultiByte(byteArray.length, _encoding);
			} catch (e:EOFError){
				dispatchEvent(new CsvEvent(CsvEvent.EOF_ERROR));
			}
			/**var tempArr:Array=resultStr.split(String.fromCharCode(13));*/
			var tempArr:Array = resultStr.split("\r\n");
			tempArr.pop();
			var keyName:Array = new Array; //首行记录字段
			
			if (tempArr.length > 0){
				tempArr[0] = tempArr[0].toLowerCase();
				keyName = tempArr[0].split(",");
				
				//解析内容
				for (var i:uint = 1; i < tempArr.length; i++){
					tempArr[i] = parse(tempArr[i]);
					var tempArr2:Array = tempArr[i].split(","); //各行的内容数组
					var arr:Array = new Array;
					
					/**以首行记录作为索引，创建二维数据_records*/
					for (var j:uint = 0; j < keyName.length; j++){
						var s:String = keyName[j];
						if (tempArr2.length <= j){
							arr[s] = "";
						} else {
							tempArr2[j] = parseBack(tempArr2[j]);
							arr[s] = tempArr2[j];
						}
					}
					_records.push(arr);
				}
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
		}

		/**还原替换*/
		private function parseBack(str:String):String {
			str = str.replace(DQUOTES, "\"");
			str = str.replace(SEMICOLON, ",");
			return str;
		}

		/**替换双引号*/
		private function parse(str:String):String {
			str = str.replace("\"\"", DQUOTES);
			str = parseSemicolon(str);
			return str;
		}

		/***/
		private function parseSemicolon(str:String):String {
			var s:int = str.search("\"");
			while (s != -1){
				str = str.substr(s + 1, str.length);
				var e:int = str.search("\"");
				var targetIndex:int = str.search(",");
				if (targetIndex != -1 && e != -1 && targetIndex < e){
					str = str.substring(0, e) + str.substr(e + 1, str.length);
					targetIndex = str.search(",");
					str = str.substring(0, targetIndex) + SEMICOLON + str.substr(targetIndex + 1, str.length);
				}
				s = str.search("\"");
			}
			return str;
		}

		/**
		 * 返回随机指定长度
		 * @param nums
		 */
		public function getRandomRecords(nums:uint):Array {
			var tempArr:Array = new Array();
			for (var i:uint = 0; i < _records.length; i++){
				tempArr.push(i);
			}
			tempArr = randArray(tempArr);
			var targetArr:Array = new Array();
			for (i = 0; i < nums; i++){
				var id:uint = tempArr[i];
				if (i >= tempArr.length){
					break;
				}
				targetArr.push(_records[id]);
			}
			return targetArr;
		}

		/*随机打乱数组*/
		private function randArray(arr:Array):Array {
			var temp:Array = new Array;
			var rand:Number = 0;
			while (arr.length > 0){
				rand = Math.floor(Math.random() * arr.length);
				temp.push(arr[rand]);
				arr.splice(rand, 1);
			}
			return temp;
		}

		/**
		 * 设置字符编码
		 */
		public function setEnCoding(code:String):void {
			_encoding = code;
		}
		/**文件IO出错*/
		private function onLoadErrorHandler(event:IOErrorEvent):void {
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
	}
}